﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.Xml;

namespace GradeManager.Web
{
    // 注意: 使用“重构”菜单上的“重命名”命令，可以同时更改代码、svc 和配置文件中的类名“Service1”。
    public class Service1 : IService1
    {
        //定义路径
        public static string strBasePath = System.AppDomain.CurrentDomain.BaseDirectory;
        public bool StoreCourseGrade(string courseID, string content)
        {
            XmlElement root, item, tmpItem;
            XmlDocument xmldoc = new XmlDocument();
            xmldoc.Load(strBasePath + "xml/" + "courseGrade" + courseID + "Sort.xml");
            root = (XmlElement)xmldoc.DocumentElement;

            //移除所有元素
            root.RemoveAll();

            string[] tmpList = content.Split(' ');
            //添加元素
            for (int i = 0; i < tmpList.Length-1; i+=2)
            {
                item = xmldoc.CreateElement("item");

                tmpItem = xmldoc.CreateElement("ID");
                tmpItem.InnerText = tmpList[i];
                item.AppendChild(tmpItem);

                tmpItem = xmldoc.CreateElement("grade");
                tmpItem.InnerText = tmpList[i+1];
                item.AppendChild(tmpItem);

                root.AppendChild(item);
            }

            //保存
            xmldoc.Save(strBasePath + "xml/" + "courseGrade" + courseID + "Sort.xml");

            return true;
        }

        public bool StoreGoodStu(string content)
        {
            XmlElement root, item, tmpItem;
            XmlDocument xmldoc = new XmlDocument();
            xmldoc.Load(strBasePath + "xml/goodStudents.xml");
            root = (XmlElement)xmldoc.DocumentElement;

            //移除所有元素
            root.RemoveAll();

            string[] tmpList = content.Split(' ');
            //添加元素
            for (int i = 0; i < tmpList.Length; i++)
            {
                item = xmldoc.CreateElement("item");

                tmpItem = xmldoc.CreateElement("ID");
                tmpItem.InnerText = tmpList[i];
                item.AppendChild(tmpItem);

                root.AppendChild(item);
            }

            //保存
            xmldoc.Save(strBasePath + "xml/goodStudents.xml");

            return true;
        }
        public bool CourseStatistics(string content)
        {
            XmlElement root, item, tmpItem;
            XmlDocument xmldoc = new XmlDocument();
            xmldoc.Load(strBasePath + "xml/courseStatistics.xml");
            root = (XmlElement)xmldoc.DocumentElement;

            //移除所有元素
            root.RemoveAll();

            string[] tmpList = content.Split(' ');
            //添加元素
            for (int i = 0; i < tmpList.Length-1; i+=5)
            {
                item = xmldoc.CreateElement("item");

                tmpItem = xmldoc.CreateElement("courseID");
                tmpItem.InnerText = tmpList[i];
                item.AppendChild(tmpItem);

                tmpItem = xmldoc.CreateElement("failNum");
                tmpItem.InnerText = tmpList[i + 1];
                item.AppendChild(tmpItem);

                tmpItem = xmldoc.CreateElement("maxGrade");
                tmpItem.InnerText = tmpList[i + 2];
                item.AppendChild(tmpItem);

                tmpItem = xmldoc.CreateElement("minGrade");
                tmpItem.InnerText = tmpList[i + 3];
                item.AppendChild(tmpItem);

                tmpItem = xmldoc.CreateElement("avgGrade");
                tmpItem.InnerText = tmpList[i + 4];
                item.AppendChild(tmpItem);

                root.AppendChild(item);
            }

            //保存
            xmldoc.Save(strBasePath + "xml/courseStatistics.xml");

            return true;
        }
        public string LoadStuXML(string fileName)
        {
            string content = "";

            XmlElement root;
            XmlDocument xmldoc = new XmlDocument();
            xmldoc.Load(strBasePath + "xml/" + fileName + ".xml");
            root = (XmlElement)xmldoc.DocumentElement;

            XmlNodeList nodes = root.SelectNodes("/items/item");
            foreach (XmlNode node in nodes)
            {
                content += node.SelectSingleNode("ID").InnerText + " ";
                content += node.SelectSingleNode("password").InnerText + " ";
            }

            return content;
        }
        public string LoadTeaXML(string fileName)
        {
            string content = "";

            XmlElement root;
            XmlDocument xmldoc = new XmlDocument();
            xmldoc.Load(strBasePath + "xml/" + fileName + ".xml");
            root = (XmlElement)xmldoc.DocumentElement;

            XmlNodeList nodes = root.SelectNodes("/items/item");
            foreach (XmlNode node in nodes)
            {
                content += node.SelectSingleNode("ID").InnerText + " ";
                content += node.SelectSingleNode("password").InnerText + " ";
            }

            return content;
        }
        public string LoadManXML(string fileName)
        {
            string content = "";

            XmlElement root;
            XmlDocument xmldoc = new XmlDocument();
            xmldoc.Load(strBasePath + "xml/" + fileName + ".xml");
            root = (XmlElement)xmldoc.DocumentElement;

            XmlNodeList nodes = root.SelectNodes("/items/item");
            foreach (XmlNode node in nodes)
            {
                content += node.SelectSingleNode("ID").InnerText + " ";
                content += node.SelectSingleNode("password").InnerText + " ";
            }

            return content;
        }
        public string LoadCourseXML(string fileName)
        {
            string content = "";

            XmlElement root;
            XmlDocument xmldoc = new XmlDocument();
            xmldoc.Load(strBasePath + "xml/" + fileName + ".xml");
            root = (XmlElement)xmldoc.DocumentElement;

            XmlNodeList nodes = root.SelectNodes("/items/item");
            foreach (XmlNode node in nodes)
            {
                content += node.SelectSingleNode("ID").InnerText + " ";
                content += node.SelectSingleNode("name").InnerText + " ";
                content += node.SelectSingleNode("credit").InnerText + " ";
                content += node.SelectSingleNode("teacherID").InnerText + " ";
            }

            return content;
        }
        public string LoadScoreXML()
        {
            string content = "";

            XmlElement root;
            XmlDocument xmldoc = new XmlDocument();

            XmlElement _root;
            XmlDocument _xmldoc = new XmlDocument();

            xmldoc.Load(strBasePath + "xml/" + "course" + ".xml");
            root = (XmlElement)xmldoc.DocumentElement;

            XmlNodeList nodes = root.SelectNodes("/items/item");
            foreach (XmlNode node in nodes)
            {
                string courseID = node.SelectSingleNode("ID").InnerText;

                _xmldoc.Load(strBasePath + "xml/courseGrade" + courseID + ".xml");
                _root = (XmlElement)_xmldoc.DocumentElement;

                XmlNodeList _nodes = _root.SelectNodes("/items/item");
                foreach (XmlNode _node in _nodes)
                {
                    content += courseID + " ";
                    content += _node.SelectSingleNode("ID").InnerText + " ";
                    content += _node.SelectSingleNode("grade").InnerText + " ";
                }
            }

            return content;
        }

        public void BackScoreXML(string courseID, string studentID, string grade)
        {
            XmlElement root;
            XmlDocument xmldoc = new XmlDocument();

            xmldoc.Load(strBasePath + "xml/courseGrade" + courseID + ".xml");
            root = (XmlElement)xmldoc.DocumentElement;

            XmlNodeList nodes = root.SelectNodes("/items/item");
            foreach (XmlNode node in nodes)
            {
                if (node.SelectSingleNode("ID").InnerText == studentID)
                {
                    node.SelectSingleNode("grade").InnerText = grade;
                    break;
                }
            }
            //保存
            xmldoc.Save(strBasePath + "xml/courseGrade" + courseID + ".xml");
        }
    }
}
